<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60%" @confirm="handleSubmit" @close="handleClose">
            <div class="mb-1">
                <a href="https://ihjmf.com/免赔导入模板.xlsx" target="_blank">下载导入模板</a>
            </div>

            <el-upload class="upload-demo" drag :action="importUploadUrl" :headers="headers" :on-success="handleSuccess" multiple>
                <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                <div class="el-upload__text">Drop file here or <em>click to upload</em></div>
                <template #tip>
                    <div class="el-upload__tip">jpg/png files with a size less than 500kb</div>
                </template>
            </el-upload>

            <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
                <el-tab-pane label="正常" name="first"></el-tab-pane>
                <el-tab-pane label="错误" name="second"></el-tab-pane>
            </el-tabs>

            <el-table v-if="activeName === 'first'" :data="state.normal" border stripe max-height="600px">
                <el-table-column label="型号" prop="type" />

                <el-table-column label="品牌" prop="brand" />
                <el-table-column label="机型" prop="spec" />
                <el-table-column label="内存" prop="disk" />
                <el-table-column label="换新价" prop="huanxin_money" />
                <el-table-column label="延保" prop="yanbao_money" />
                <el-table-column label="电池" prop="dianchi_money" />
                <el-table-column label="碎屏" prop="suiping_money" />
            </el-table>

            <el-table v-if="activeName === 'second'" :data="state.error" border stripe max-height="600px">
                <el-table-column label="型号" prop="type" />

                <el-table-column label="品牌" prop="brand" />
                <el-table-column label="机型" prop="spec" />
                <el-table-column label="内存" prop="disk" />
                <el-table-column label="换新价" prop="huanxin_money" />
                <el-table-column label="延保" prop="yanbao_money" />
                <el-table-column label="电池" prop="dianchi_money" />
                <el-table-column label="碎屏" prop="suiping_money" />

                <el-table-column label="错误信息" prop="error_msg" width="200" />
            </el-table>
        </popup>
    </div>
</template>

<script lang="ts" setup name="cardEdit">
import { apiHuanxinSureImport } from '@/api/huanxin'

import type { TabsPaneContext } from 'element-plus'

const activeName = ref('first')

const handleClick = (tab: TabsPaneContext, event: Event) => {
    console.log(tab, event)
}

import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import type { PropType } from 'vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()

import { ref } from 'vue'
import { genFileId } from 'element-plus'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'

const upload = ref<UploadInstance>()
import useUserStore from '@/stores/modules/user'
import config from '@/config'
const userStore = useUserStore()
const headers = computed(() => ({
    token: userStore.token,
    version: config.version
}))
const importUploadUrl = ref(`${config.baseUrl}${config.urlPrefix}/huanxin/import`)

const handleSuccess = (data: any) => {
    state.normal = data.data.normal
    state.error = data.data.error
}

// 弹窗标题
const popupTitle = computed(() => {
    return '导入免赔配置'
})

const state = reactive({
    normal: [],
    error: []
})

const handleSubmit = async () => {
    await apiHuanxinSureImport(state.normal)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    popupRef.value?.open()
    state.normal = []
    state.error = []
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

defineExpose({
    open
})
</script>
